/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package jsf;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;
import javax.faces.FacesException;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.model.SelectItem;
import jpa.controladores.DatosPersonalesJpaController;
import jpa.entidades.DatosPersonales;
import jsf.util.JsfUtil;
import jpa.controladores.exceptions.NonexistentEntityException;
import jsf.util.PagingInfo;

/**
 *
 * @author alphyon
 */
public class DatosPersonalesController {

    public DatosPersonalesController() {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        jpaController = (DatosPersonalesJpaController) facesContext.getApplication().getELResolver().getValue(facesContext.getELContext(), null, "datosPersonalesJpa");
        pagingInfo = new PagingInfo();
        converter = new DatosPersonalesConverter();
    }
    private DatosPersonales datosPersonales = null;
    private List<DatosPersonales> datosPersonalesItems = null;
    private DatosPersonalesJpaController jpaController = null;
    private DatosPersonalesConverter converter = null;
    private PagingInfo pagingInfo = null;

    public PagingInfo getPagingInfo() {
        if (pagingInfo.getItemCount() == -1) {
            pagingInfo.setItemCount(jpaController.getDatosPersonalesCount());
        }
        return pagingInfo;
    }

    public SelectItem[] getDatosPersonalesItemsAvailableSelectMany() {
        return JsfUtil.getSelectItems(jpaController.findDatosPersonalesEntities(), false);
    }

    public SelectItem[] getDatosPersonalesItemsAvailableSelectOne() {
        return JsfUtil.getSelectItems(jpaController.findDatosPersonalesEntities(), true);
    }

    public DatosPersonales getDatosPersonales() {
        if (datosPersonales == null) {
            datosPersonales = (DatosPersonales) JsfUtil.getObjectFromRequestParameter("jsfcrud.currentDatosPersonales", converter, null);
        }
        if (datosPersonales == null) {
            datosPersonales = new DatosPersonales();
        }
        return datosPersonales;
    }

    public String listSetup() {
        reset(true);
        return "datosPersonales_list";
    }

    public String createSetup() {
        reset(false);
        datosPersonales = new DatosPersonales();
        return "datosPersonales_create";
    }

    public String create() {
        try {
            jpaController.create(datosPersonales);
            JsfUtil.addSuccessMessage("DatosPersonales was successfully created.");
        } catch (Exception e) {
            JsfUtil.ensureAddErrorMessage(e, "A persistence error occurred.");
            return null;
        }
        return listSetup();
    }

    public String detailSetup() {
        return scalarSetup("datosPersonales_detail");
    }

    public String editSetup() {
        return scalarSetup("datosPersonales_edit");
    }

    private String scalarSetup(String destination) {
        reset(false);
        datosPersonales = (DatosPersonales) JsfUtil.getObjectFromRequestParameter("jsfcrud.currentDatosPersonales", converter, null);
        if (datosPersonales == null) {
            String requestDatosPersonalesString = JsfUtil.getRequestParameter("jsfcrud.currentDatosPersonales");
            JsfUtil.addErrorMessage("The datosPersonales with id " + requestDatosPersonalesString + " no longer exists.");
            return relatedOrListOutcome();
        }
        return destination;
    }

    public String edit() {
        String datosPersonalesString = converter.getAsString(FacesContext.getCurrentInstance(), null, datosPersonales);
        String currentDatosPersonalesString = JsfUtil.getRequestParameter("jsfcrud.currentDatosPersonales");
        if (datosPersonalesString == null || datosPersonalesString.length() == 0 || !datosPersonalesString.equals(currentDatosPersonalesString)) {
            String outcome = editSetup();
            if ("datosPersonales_edit".equals(outcome)) {
                JsfUtil.addErrorMessage("Could not edit datosPersonales. Try again.");
            }
            return outcome;
        }
        try {
            jpaController.edit(datosPersonales);
            JsfUtil.addSuccessMessage("DatosPersonales was successfully updated.");
        } catch (NonexistentEntityException ne) {
            JsfUtil.addErrorMessage(ne.getLocalizedMessage());
            return listSetup();
        } catch (Exception e) {
            JsfUtil.ensureAddErrorMessage(e, "A persistence error occurred.");
            return null;
        }
        return detailSetup();
    }

    public String destroy() {
        String idAsString = JsfUtil.getRequestParameter("jsfcrud.currentDatosPersonales");
        Integer id = new Integer(idAsString);
        try {
            jpaController.destroy(id);
            JsfUtil.addSuccessMessage("DatosPersonales was successfully deleted.");
        } catch (NonexistentEntityException ne) {
            JsfUtil.addErrorMessage(ne.getLocalizedMessage());
            return relatedOrListOutcome();
        } catch (Exception e) {
            JsfUtil.ensureAddErrorMessage(e, "A persistence error occurred.");
            return null;
        }
        return relatedOrListOutcome();
    }

    private String relatedOrListOutcome() {
        String relatedControllerOutcome = relatedControllerOutcome();
        if (relatedControllerOutcome != null) {
            return relatedControllerOutcome;
        }
        return listSetup();
    }

    public List<DatosPersonales> getDatosPersonalesItems() {
        if (datosPersonalesItems == null) {
            getPagingInfo();
            datosPersonalesItems = jpaController.findDatosPersonalesEntities(pagingInfo.getBatchSize(), pagingInfo.getFirstItem());
        }
        return datosPersonalesItems;
    }

    public String next() {
        reset(false);
        getPagingInfo().nextPage();
        return "datosPersonales_list";
    }

    public String prev() {
        reset(false);
        getPagingInfo().previousPage();
        return "datosPersonales_list";
    }

    private String relatedControllerOutcome() {
        String relatedControllerString = JsfUtil.getRequestParameter("jsfcrud.relatedController");
        String relatedControllerTypeString = JsfUtil.getRequestParameter("jsfcrud.relatedControllerType");
        if (relatedControllerString != null && relatedControllerTypeString != null) {
            FacesContext context = FacesContext.getCurrentInstance();
            Object relatedController = context.getApplication().getELResolver().getValue(context.getELContext(), null, relatedControllerString);
            try {
                Class<?> relatedControllerType = Class.forName(relatedControllerTypeString);
                Method detailSetupMethod = relatedControllerType.getMethod("detailSetup");
                return (String) detailSetupMethod.invoke(relatedController);
            } catch (ClassNotFoundException e) {
                throw new FacesException(e);
            } catch (NoSuchMethodException e) {
                throw new FacesException(e);
            } catch (IllegalAccessException e) {
                throw new FacesException(e);
            } catch (InvocationTargetException e) {
                throw new FacesException(e);
            }
        }
        return null;
    }

    private void reset(boolean resetFirstItem) {
        datosPersonales = null;
        datosPersonalesItems = null;
        pagingInfo.setItemCount(-1);
        if (resetFirstItem) {
            pagingInfo.setFirstItem(0);
        }
    }

    public void validateCreate(FacesContext facesContext, UIComponent component, Object value) {
        DatosPersonales newDatosPersonales = new DatosPersonales();
        String newDatosPersonalesString = converter.getAsString(FacesContext.getCurrentInstance(), null, newDatosPersonales);
        String datosPersonalesString = converter.getAsString(FacesContext.getCurrentInstance(), null, datosPersonales);
        if (!newDatosPersonalesString.equals(datosPersonalesString)) {
            createSetup();
        }
    }

    public Converter getConverter() {
        return converter;
    }

}
